package com.props.scorer;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.props.scorer.R;
import com.props.scorer.constants.SharedPreferenceConstants;

public class settingsActivity extends Activity {
	private EditText etPlayer1Name;
	private EditText etPlayer2Name;
	private EditText etPlayer3Name;
	private EditText etPlayer4Name;
	private CheckBox cbOnBoardMultipliers;
	private CheckBox cbCardMultipliers;
	private CheckBox cbClearing;
	private CheckBox cbFourCardClear;
	private CheckBox cbFiveCardClear;
	private CheckBox cbLocking;
	private CheckBox cbPlayerThree;
	private CheckBox cbPlayerFour;
	private Button buttonSave;
	private Button buttonCancel;
	private PropsApplication propsApplication;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settingsform);
        
        //Bring application object into view
        propsApplication = (PropsApplication) getApplicationContext();
        
        getFormFields();
        setInitialFieldValues();
        setOnclickHandlers();
        toggleClearingOptions();
        enableEditTextByCheckBox(cbPlayerThree, etPlayer3Name);
        enableEditTextByCheckBox(cbPlayerFour, etPlayer4Name);
    }
    
    private void showSettingsSavedAlert()
    {
    	AlertDialog.Builder savedAlert = new AlertDialog.Builder(this);
    	savedAlert.setIcon(android.R.drawable.ic_dialog_alert);
    	savedAlert.setTitle(R.string.settingsSavedAlertTitle);
    	savedAlert.setMessage(R.string.settingsSavedAlertMessage);
    	savedAlert.setPositiveButton(R.string.settingsSavedOk, saveDialogClickListener);
    	savedAlert.show();
    }
    
    private void showCancelSavedAlert()
    {
    	AlertDialog.Builder cancelAlert = new AlertDialog.Builder(this);
    	cancelAlert.setIcon(android.R.drawable.ic_dialog_alert);
    	cancelAlert.setTitle(R.string.cancelSettingsAlertTitle);
    	cancelAlert.setMessage(R.string.cancelSettingsAlertMessage);
    	cancelAlert.setPositiveButton(R.string.confirmCancelSettings, cancelDialogClickListener);
    	cancelAlert.setNegativeButton(R.string.doNotCancelSettings, cancelDialogClickListener);
    	cancelAlert.show();
    }
    
    private void getFormFields()
    {
    	etPlayer1Name = (EditText) findViewById(R.id.etPlayerOneName);
    	etPlayer2Name = (EditText) findViewById(R.id.etPlayerTwoName);
    	etPlayer3Name = (EditText) findViewById(R.id.etPlayerThreeName);
    	etPlayer4Name = (EditText) findViewById(R.id.etPlayerFourName);
    	
    	cbOnBoardMultipliers = (CheckBox) findViewById(R.id.cbOnBoardMultipliers);
    	cbCardMultipliers = (CheckBox) findViewById(R.id.cbCardMultipliers);
    	cbClearing = (CheckBox) findViewById(R.id.cbClearing);
    	cbFourCardClear = (CheckBox) findViewById(R.id.cbFourCardClearing);
    	cbFiveCardClear = (CheckBox) findViewById(R.id.cbFiveCardClearing);
    	cbLocking = (CheckBox) findViewById(R.id.cbLocking);
    	cbPlayerThree = (CheckBox) findViewById(R.id.cbPlayerThree);
    	cbPlayerFour = (CheckBox) findViewById(R.id.cbPlayerFour);
    	
    	buttonSave = (Button) findViewById(R.id.saveButton);
    	buttonCancel = (Button) findViewById(R.id.cancelButton);
    }
    
    private void setInitialFieldValues()
    {
    	cbOnBoardMultipliers.setChecked(propsApplication.userSettings.isbBoardMultiplierEnabled());
    	cbCardMultipliers.setChecked(propsApplication.userSettings.isbCardMultiplierEnabled());
    	cbClearing.setChecked(propsApplication.userSettings.isbClearingEnabled());
    	cbFourCardClear.setChecked(propsApplication.userSettings.isbFourCardCancelLockEnabled());
    	cbFiveCardClear.setChecked(propsApplication.userSettings.isbFiveCardSelfClearEnabled());
    	cbLocking.setChecked(propsApplication.userSettings.isbLockingEnabled());
    	cbPlayerThree.setChecked(propsApplication.userSettings.isbPlayerThreeEnabled());
    	cbPlayerFour.setChecked(propsApplication.userSettings.isbPlayerFourEnabled());
    	
    	etPlayer1Name.setText(propsApplication.userSettings.getsPlayerOneName());
    	etPlayer2Name.setText(propsApplication.userSettings.getsPlayerTwoName());
    	etPlayer3Name.setText(propsApplication.userSettings.getsPlayerThreeName());
    	etPlayer4Name.setText(propsApplication.userSettings.getsPlayerFourName());
    }
    
    private void saveSettings()
    {
    	int iNumOfScoreCards = SharedPreferenceConstants.USER_PREF_DEFAULT_NUM_OF_SCORE_CARDS,
    		iNumOfMultiplierCards = SharedPreferenceConstants.USER_PREF_DEFAULT_NUM_OF_MULTIPLIER_CARDS,
    		iNumOfLockCards = SharedPreferenceConstants.USER_PREF_DEFAULT_NUM_OF_LOCK_CARDS,
    		iMultiplierCardSingleValue = SharedPreferenceConstants.USER_PREF_DEFAULT_SINGLE_MULT_CARD_VALUE,
    		iMultiplierCardDoubleValue = SharedPreferenceConstants.USER_PREF_DEFAULT_DOUBLE_MULT_CARD_VALUE,
    		iMultiplierCardOnBoardValue = SharedPreferenceConstants.USER_PREF_DEFAULT_ON_BOARD_MULT_VALUE;

    	boolean bCardMultiplierEnabled = cbCardMultipliers.isChecked(),
    			bBoardMultiplierEnabled = cbOnBoardMultipliers.isChecked(),
    			bClearingEnabled = cbClearing.isChecked(),
    			bLockingEnabled = cbLocking.isChecked(),
    			bFourCardCancelLockEnabled = cbFourCardClear.isChecked(),
    			bFiveCardSelfClearEnabled = cbFiveCardClear.isChecked(),
    			bPlayerThreeEnabled = cbPlayerThree.isChecked(),
    			bPlayerFourEnabled = cbPlayerFour.isChecked();

    	String 	sPlayerOneName = etPlayer1Name.getText().toString(),
    			sPlayerTwoName = etPlayer2Name.getText().toString(),
    			sPlayerThreeName = etPlayer3Name.getText().toString(),
    			sPlayerFourName = etPlayer4Name.getText().toString();
   
    	propsApplication.userSettings.UpdateUserSettings(	iNumOfScoreCards, iNumOfMultiplierCards, 
    														iNumOfLockCards, iMultiplierCardSingleValue, 
    														iMultiplierCardDoubleValue, iMultiplierCardOnBoardValue, 
    														bCardMultiplierEnabled, bBoardMultiplierEnabled, 
    														bClearingEnabled, bLockingEnabled, 
    														bFourCardCancelLockEnabled, bFiveCardSelfClearEnabled, 
    														sPlayerOneName, sPlayerTwoName, 
    														sPlayerThreeName, sPlayerFourName, 
    														bPlayerThreeEnabled, bPlayerFourEnabled);
    	
    	propsApplication.updateSharedPreferences();
    }
    
    private void setOnclickHandlers()
    {
        buttonSave.setOnClickListener(oclSaveButtonClick);
        buttonCancel.setOnClickListener(oclCancelButtonClick);
        cbPlayerThree.setOnClickListener(oclPlayerThreeCbClick);
        cbPlayerFour.setOnClickListener(oclPlayerFourCbClick);
        cbClearing.setOnClickListener(oclClearingCbClick);
    }
    
    //Toggle the enabled/checked properties on options dependent on cbClearing when the CheckBox is clicked.
    private void toggleClearingOptions()
    {
    	cbFourCardClear.setEnabled(cbClearing.isChecked());
    	cbFiveCardClear.setEnabled(cbClearing.isChecked());
    	cbLocking.setEnabled(cbClearing.isChecked());
    	
    	if (!cbClearing.isChecked())
    	{
    		cbFourCardClear.setChecked(false);
    		cbFiveCardClear.setChecked(false);
    		cbLocking.setChecked(false);
    	}
    }
    
    //Set the enabled property of EditText eb based on the isChecked property of CheckBox cb.
    private void enableEditTextByCheckBox(CheckBox cb, EditText et)
    {
    	et.setEnabled(cb.isChecked());
    	
    	if (!cb.isChecked())
    	{
    		et.setText(null);
    	}
    }
    
    //Go to Game Setup (main screen)
    private void goToGameSetupActivity()
    {
        Intent myIntent = new Intent(settingsActivity.this, gameSetupActivity.class);
        startActivityForResult(myIntent, 0);
        finish();
    }
    
    //OnClickListeners
    private OnClickListener oclSaveButtonClick = new OnClickListener() {
    	public void onClick(View view) {
    		showSettingsSavedAlert();
        }
    };
    
    //OnClickListeners
    private OnClickListener oclCancelButtonClick = new OnClickListener() {
    	public void onClick(View view) {
    		showCancelSavedAlert();
        }
    };

    private OnClickListener oclPlayerThreeCbClick = new OnClickListener() {
    	public void onClick(View view) {
    		if (cbPlayerFour.isChecked())
    		{
    			cbPlayerFour.setChecked(false);
        		enableEditTextByCheckBox(cbPlayerFour, etPlayer4Name);
    		}
    		
    		enableEditTextByCheckBox(cbPlayerThree, etPlayer3Name);
    		
        }
    };
    
    private OnClickListener oclPlayerFourCbClick = new OnClickListener() {
    	public void onClick(View view) {
    		if (cbPlayerThree.isChecked())
    		{
    			enableEditTextByCheckBox(cbPlayerFour, etPlayer4Name);
    		}
    		else
    		{
    			cbPlayerFour.setChecked(false);
    		}
        }
    };
    
    private OnClickListener oclClearingCbClick = new OnClickListener() {
    	public void onClick(View view) {
    		toggleClearingOptions();
        }
    };
    
    DialogInterface.OnClickListener saveDialogClickListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
            case DialogInterface.BUTTON_POSITIVE:
        		saveSettings();
        		goToGameSetupActivity();
                break;
            }
        }
    };

    DialogInterface.OnClickListener cancelDialogClickListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
            case DialogInterface.BUTTON_POSITIVE:
         		goToGameSetupActivity();
                break;

            case DialogInterface.BUTTON_NEGATIVE:
                //No button clicked - keep them on the page to allow save
                break;
            }
        }
    };

}
